define(function(require){

    require('../../../c/base/index');

    require('../../../c/nav/index');


    var blackUser = {
        init : function() {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function(){
                if($(this).parents("a").text() == "用户黑名单"){
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.bindEvent();
        },

        bindEvent : function() {
            var self = this;

            $("#add").click(function () {
                $("#showResults").empty();
                var mobiles = $(".form-control").val();
                if (mobiles) {
                    var arr = mobiles.split("\n");
                    for (var m in arr) {
                        self.addBlackUserList(arr[m])
                    }
                }
            });

            $("#get").click(function () {
                $("#showResults").empty();
                var mobiles = $(".form-control").val();
                if (mobiles) {
                    var arr = mobiles.split("\n");
                    for (var m in arr) {
                        self.isBlackUserList(arr[m]);
                    }
                }
            });

            $("#del").click(function () {
                $("#showResults").empty();
                var mobiles = $(".form-control").val();
                if (mobiles) {
                    var arr = mobiles.split("\n");
                    for (var m in arr) {
                        self.delBlackUserList(arr[m]);
                    }
                }
            });
        },

        addBlackUserList : function(mobile) {
            CRM.request({
                url: CRM.getOuterUrl('/member/saveUserBlackList.do'),
                type: 'post',
                dataType: 'json',
                async: false,
                data: JSON.stringify({
                    "mobile": mobile
                }),
                error: function (err) {
                    var dis = "将手机号码:" + mobile + "添加到黑名单时出错了!";
                    alert(dis);
                    $("#showResults").append('<div class="alert alert-danger">' + dis
                        + '</div>');
                },
                success: function (data) {
                    var dis = mobile + ' ' + data.message;
                    if (data.status) {
                        $("#showResults").append('<div class="alert alert-warning">' + dis
                            + '</div>');
                    } else {
                        $("#showResults").append('<div class="alert alert-danger">' + dis
                            + '</div>');
                    }
                }
            });
        },
        isBlackUserList : function(mobile) {
            CRM.request({
                url: CRM.getOuterUrl('/member/isBlackListUser.do'),
                type: 'post',
                dataType: 'json',
                async: false,
                data: JSON.stringify({
                    "mobile": mobile
                }),
                error: function (error) {
                    var dis = "查询手机号:" + mobile + " 出错了!";
                    $("#showResults").append('<div class="alert alert-danger">' + dis
                        + '</div>');
                },
                success: function (data) {
                    var dis = mobile + ' ' + data.message;
                    if (data.status) {
                        if (data.entry) {
                            $("#showResults").append('<div class="alert alert-warning">' + dis
                                + '</div>');
                        } else {
                            $("#showResults").append('<div class="alert alert-success">' + dis
                                + '</div>');
                        }
                    } else {
                        $("#showResults").append('<div class="alert alert-danger">' + dis
                            + '</div>');
                    }
                }
            });
        },
        delBlackUserList : function(mobile) {
            CRM.request({
                url: CRM.getOuterUrl('/member/deleteUserBlackList.do'),
                type: 'post',
                dataType: 'json',
                async: false,
                data: JSON.stringify({
                    "mobile": mobile
                }),
                error: function (error) {
                    var dis = "将手机号:" + mobile + " 移出黑名单出错了!";
                    $("#showResults").append('<div class="alert alert-danger">' + dis
                        + '</div>');
                },
                success: function (data) {
                    var dis = mobile + ' ' + data.message;
                    if (data.status) {
                        $("#showResults").append('<div class="alert alert-success">' + dis
                            + '</div>');
                    } else {
                        $("#showResults").append('<div class="alert alert-danger">' + dis
                            + '</div>');
                    }
                }
            });
        }
    };

    blackUser.init();
});

